package com.vm.osm.business.service.impl;

import com.vm.osm.business.common.Constants;
import com.vm.osm.business.dao.OsmSysConfigMapper;
import com.vm.osm.business.dto.rsp.authRule.RspConfig;
import com.vm.osm.business.entity.OsmSysConfig;
import com.vm.osm.business.service.IOsmSysConfigService;
import com.vm.osm.business.util.UserDataUtil;
import com.vm.osm.business.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * Created by chenzui on 2018/3/9.
 */
@Service
public class OsmSysConfigServiceImpl implements IOsmSysConfigService {

    @Autowired
    private OsmSysConfigMapper osmSysConfigMapper;

    @Override
    public int deleteByPrimaryKey(String id) {
        return osmSysConfigMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(OsmSysConfig record) {
        return osmSysConfigMapper.insert(record);
    }

    @Override
    public int insertSelective(OsmSysConfig record) {
        return osmSysConfigMapper.insertSelective(record);
    }

    @Override
    public OsmSysConfig selectByPrimaryKey(String id) {
        return osmSysConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(OsmSysConfig record) {
        return osmSysConfigMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(OsmSysConfig record) {
        return osmSysConfigMapper.updateByPrimaryKey(record);
    }

//    @Override
//    public OsmSysConfig queryByCfgCode(String cfgCode) {
//        return osmSysConfigMapper.queryByCfgCode(cfgCode);
//    }

    @Override
    public void save(String dispatch, String condition,HttpServletRequest request) {
        OsmSysConfig dispatchCfg = osmSysConfigMapper.queryByCfgCode(Constants.CFG_CODE_DISPATCH, UserDataUtil.getUserSelectedEnterpriseId(request));

        OsmSysConfig conditionCfg = osmSysConfigMapper.queryByCfgCode(Constants.CFG_CODE_CONDITION,UserDataUtil.getUserSelectedEnterpriseId(request));

        if(dispatchCfg != null){
            dispatchCfg.setStatus(new Byte(dispatch));
            osmSysConfigMapper.updateByPrimaryKey(dispatchCfg);
        }else {
            dispatchCfg = new OsmSysConfig();
            dispatchCfg.setStatus(new Byte(dispatch));
            dispatchCfg.setUpdateTime(new Date());
            dispatchCfg.setCfgCode(Constants.CFG_CODE_DISPATCH);
            dispatchCfg.setCreateTime(new Date());
            dispatchCfg.setEnterpriseId(UserDataUtil.getUserSelectedEnterpriseId(request));
            dispatchCfg.setId(UuidUtil.getRandomUuid());
            osmSysConfigMapper.insertSelective(dispatchCfg);
        }
        if(conditionCfg != null){
            conditionCfg.setStatus(new Byte(condition));
            osmSysConfigMapper.updateByPrimaryKey(conditionCfg);
        }else {
            conditionCfg = new OsmSysConfig();
            conditionCfg.setStatus(new Byte(condition));
            conditionCfg.setUpdateTime(new Date());
            conditionCfg.setCreateTime(new Date());
            conditionCfg.setCfgCode(Constants.CFG_CODE_CONDITION);
            conditionCfg.setEnterpriseId(UserDataUtil.getUserSelectedEnterpriseId(request));
            conditionCfg.setId(UuidUtil.getRandomUuid());
            osmSysConfigMapper.insertSelective(conditionCfg);
        }
    }

    @Override
    public RspConfig get(HttpServletRequest request) {
        OsmSysConfig dispatchCfg = osmSysConfigMapper.queryByCfgCode(Constants.CFG_CODE_DISPATCH, UserDataUtil.getUserSelectedEnterpriseId(request));

        OsmSysConfig conditionCfg = osmSysConfigMapper.queryByCfgCode(Constants.CFG_CODE_CONDITION,UserDataUtil.getUserSelectedEnterpriseId(request));

        RspConfig config = new RspConfig();

        config.setDispatch(dispatchCfg!=null? dispatchCfg.getStatus().toString():"0");

        config.setCondition(conditionCfg!=null?conditionCfg.getStatus().toString():"0");

        return config;
    }
}
